
import LoadingManager from "./LoadingManager";
import ModuleManager from "../../cfw/module/ModuleManager";
import { ResType } from "../../cfw/res/ResInterface";
import ResItem from "../../cfw/res/ResItem";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName } from "../../public/Config";

export default class EnterGameLoading extends LoadingManager {



    private bundleList: number[]
    private max: number = 0;

    constructor(list:number[]){
        super();
        this.bundleList = list;
        this.max = list.length;
    }

    start() {
        // this.bundleList = ModuleManager.getBundleList()
        // this.max = this.bundleList.length;
        this.loadBundles();
    }

    loadBundles() {
        const max: number = this.max;
        cc.log('this.bundleList.length  ',this.bundleList.length)
        if (this.bundleList.length > 0) {
            let modelID = this.bundleList.shift();
            ModuleManager.publicLoader().loadRes(ModuleManager.getName(modelID), ResType.AssetBundle, (err, item: ResItem) => {
                if (err) {
                    cc.error(' LoadingManger error ', err)
                    return;
                }
                ModuleManager.setLoaderByBundle(modelID, item.getRes())
                let percent = (max - this.bundleList.length) / max
                cc.log('loadBundles  percent ', percent, ' this.bundleList.length  ', this.bundleList.length, ' max ', max)
                GlobalEvent.instance().emit(EventName.UPDATE_LOADING_PERCENT, percent)
                this.loadBundles()
            })
        } else {

        }

    }
}
